using System;
using System.Xml.Serialization;

namespace SharpMud.MudLib.Core.Time
{
	[XmlRoot("MudTimeConfiguration")]
	public class MudTimeConfiguration
	{
		#region Static Globalization
		public static MudTimeConfiguration Default
		{
			get
			{
				MudTimeConfiguration mtc = new MudTimeConfiguration();

				mtc.RealSecondsPerTick	= 1.5;
				
				//Makes a game hour 20 *real* minutes
				mtc.TicksPerHour		= (int)((((double)1 / mtc.RealSecondsPerTick)*(double)60)*(double)20);

				//24 *game* hours per *game* day
				mtc.HoursPerDay = 24;

				//30 *game* days per *game* month
				mtc.DaysPerMonth = 30;

				//Set the beginning of time in *mud* time
				mtc.TotalMudUptime_TS = TimeSpan.Zero;
				//Couldn't use a month/day/year triplet, because the MudDateTime constructor depends on a
				//globally established MudTimeConfiguration. So...
				//use the Ultimate Answer to Life the Universe & Everything!
				mtc.MudTimeOrigin = new MudDateTime(424242424);
				
				return mtc;
			}
		}

		private static MudTimeConfiguration _Current;
		public static MudTimeConfiguration Current
		{
			get
			{
				if(_Current==null)
					return MudTimeConfiguration.Default;
				return _Current;
			}
		}

		public void GoGlobal()
		{
			_Current = this;
		}

		#endregion

		#region Private Variables
		private double _RealSecondsPerTick;
		private long _TicksPerHour;
		private int _HoursPerDay;
		private int _DaysPerMonth;

		private System.TimeSpan _TotalMudUptime;
		private MudDateTime		_MudTimeOrigin;
		#endregion

		#region Public Properties
		[XmlElement("RealSecondsPerTick")]
		public double RealSecondsPerTick
		{
			get
			{
				return _RealSecondsPerTick;
			}
			set
			{
				_RealSecondsPerTick = value;
			}
		}

		[XmlElement("TicksPerHour")]
		public long TicksPerHour
		{
			get
			{
				return _TicksPerHour;
			}
			set
			{
				_TicksPerHour = value;
			}
		}

		[XmlElement("HoursPerDay")]
		public int HoursPerDay
		{
			get
			{
				return _HoursPerDay;
			}
			set
			{
				_HoursPerDay = value;
			}
		}

		[XmlElement("DaysPerMonth")]
		public int DaysPerMonth
		{
			get
			{
				return _DaysPerMonth;
			}
			set
			{
				_DaysPerMonth = value;
			}
		}

		[XmlIgnore()]
		public System.TimeSpan TotalMudUptime_TS
		{
			get
			{
				return _TotalMudUptime;
			}
			set
			{
				_TotalMudUptime = value;
			}
		}

		[XmlElement("TotalMudUptime")]
		public string TotalMudUptime
		{
			get
			{
				return _TotalMudUptime.ToString();
			}
			set
			{
				_TotalMudUptime = TimeSpan.Parse(value);
			}
		}

		[XmlElement("MudTimeOrigin")]
		public MudDateTime MudTimeOrigin
		{
			get
			{
				return _MudTimeOrigin;
			}
			set
			{
				_MudTimeOrigin = value;
			}
		}
		#endregion

		#region Public (Implied) Properties
		public int DaysPerWeek
		{
			get
			{
				return Enum.GetValues(typeof(Core.Time.DayOfWeek)).Length;
			}
		}
	
		public int MonthsPerYear
		{
			get
			{
				return Enum.GetValues(typeof(Core.Time.MonthOfYear)).Length;
			}
		}
		public int DaysPerYear
		{
			get
			{
				return DaysPerMonth * MonthsPerYear;
			}
		}
		public long TicksPerDay
		{
			get
			{
				return TicksPerHour * HoursPerDay;
			}
		}
		public long TicksPerYear
		{
			get
			{
				return TicksPerDay * DaysPerYear;
			}
		}
		#endregion
	}

	public enum MonthOfYear
	{
		Spring,
		Summer,
		Autumn,
		Winter
	}

	public enum DayOfWeek
	{
		Sunday,
		Monday,
		Tuesday,
		Wednesday,
		Thursday,
		Friday,
		Saturday
	}
}
